using UnityEditor;
using UnityEngine;

namespace HEFramework
{
    /// <summary>
    ///
    /// UI窗口行为编辑面板
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-02-03 23:05:02
    /// ----------------------------------------
    /// </summary>
    [CustomEditor(typeof(UIWindowBehaviour))]
    public class UIWindowBehaviourInspector : Editor
    {
        public override void OnInspectorGUI()
        {
            GUILayout.BeginHorizontal();
            UIWindowBehaviour uiWindowBehaviour = target as UIWindowBehaviour;
            if (GUILayout.Button(new GUIContent("组件绑定"), EditorStyles.miniButton, GUILayout.Width(130)))
            {
                uiWindowBehaviour.Awake();
                uiWindowBehaviour.Transforms.Clear();
                GameObject[] objs = Selection.GetFiltered<GameObject>(SelectionMode.Deep);
                for (int i = 0; i < objs.Length; ++i)
                {
                    GameObject obj = objs[i];
                    IUIBehaviour behaviour;
                    if (obj.name.StartsWith("Canvas_") || obj.name.StartsWith("canvas_"))
                    {
                        behaviour = obj.GetComponent<IUICanvasBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UICanvasBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("Text_") || obj.name.StartsWith("text_"))
                    {
                        behaviour = obj.GetComponent<IUITextBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UITextBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("TMPText_") || obj.name.StartsWith("tmpText_"))
                    {
                        behaviour = obj.GetComponent<IUITMPTextBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UITMPTextBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("Image_") || obj.name.StartsWith("image_"))
                    {
                        behaviour = obj.GetComponent<IUIImageBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UIImageBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("Button_") || obj.name.StartsWith("button_"))
                    {
                        behaviour = obj.GetComponent<IUIButtonBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UIButtonBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("Slider_") || obj.name.StartsWith("slider_"))
                    {
                        behaviour = obj.GetComponent<IUISliderBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UISliderBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("ScrollView_") || obj.name.StartsWith("scrollView_"))
                    {
                        behaviour = obj.GetComponent<IUIScrollViewBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UIScrollViewBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("InputField_") || obj.name.StartsWith("inputField_"))
                    {
                        behaviour = obj.GetComponent<IUIInputFieldBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UIInputFieldBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("TMPInputField_") || obj.name.StartsWith("tmpInputField_"))
                    {
                        behaviour = obj.GetComponent<IUITMPInputFieldBehaviour>();
                        if (behaviour == null)
                        {
                            behaviour = obj.AddComponent<UITMPInputFieldBehaviourBase>();
                        }

                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                    else if (obj.name.StartsWith("Transform_") || obj.name.StartsWith("transform_"))
                    {
                        if (CheckName(uiWindowBehaviour, obj.name))
                        {
                            uiWindowBehaviour.Transforms.Add(obj.transform);
                        }
                    }
                }

                PrefabUtility.SavePrefabAsset(Selection.activeGameObject);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                Debug.Log("绑定组件完成！");
            }

            GUILayout.EndHorizontal();
            base.OnInspectorGUI();
        }

        private bool CheckName(UIWindowBehaviour _behaviour, string _name)
        {
            for (int i = 0; i < _behaviour.Transforms.Count; i++)
            {
                if (_behaviour.Transforms[i].name == _name)
                {
                    Debug.LogError(_behaviour.name + "-->" + _name + " exist.");
                    return false;
                }
            }

            return true;
        }
    }
}